package com.pangolin.pangolinim.net;

public class Netpackage {
    /* 命令码列表 */
    public static final int COM_HEARTBEAT         = 1;
    public static final int COM_MESSAGE           =100;
    public static final int COM_RE_MESSAGE        =101;
    public static final int COM_OFF_MESSAGE       =102;
    public static final int COM_RE_OFF_MESSAGE    =103;
    public static final int COM_REGISTER          =200;
    public static final int COM_RE_REGISTER       =201;
    public static final int COM_LOGIN             =202;
    public static final int COM_RE_LOGIN          =203;
    public static final int COM_FINDFRIEND        =204;
    public static final int COM_RE_FINDFRIEND     =205;
    public static final int COM_LOGOUT            =206;
    public static final int COM_RE_LOGOUT         =207;
    public static final int COM_REPASS            =208;
    public static final int COM_RE_REPASS         =209;
    public static final int COM_OPERATEFRIEND     =210;
    public static final int COM_RE_OPERATEFRIEND  =211;
    public static final int COM_CONFIRMFRIEND     =212;
    public static final int COM_RE_CONFIRMFRIEND  =213;
    public static final int COM_DELFRIEND         =214;
    public static final int COM_RE_DELFRIEND      =215;
    public static final int COM_FRIENDLIST        =216;
    public static final int COM_RE_FRIENDLIST     =217;
    public static final int COM_USERINFO          =218;
    public static final int COM_RE_USERINFO       =219;

    public static final int CLI_MESSAGE           =800;
    public static final int CLI_OPERATEFRIEND     =801;
    public static final int CLI_CONFIRMFRIEND     =802;
    public static final int CLI_DELFRIEND         =803;


    /* 包头结构 */
    private int PackageLen;     //包长度
    private short HeadLen;      //包头长度
    private short Ver;          //协议版本
    private int Command;        //命令码
    private int Idx;            //包序号

    private String data;        //包体内容

    public Netpackage(){

    }

    /*
        解包函数，用于结构化接收byte数据
        @src 原始包数据流
     */

    public Netpackage(byte[] src){
        formatAll(src);
    }

    /*
        封包函数，用户将数据存储到类中
        @data 包体字符串
        @cmd  包命令码
     */
    public Netpackage(int cmd , String data){
        HeadLen = 16;
        PackageLen = HeadLen + data.length();
        Ver = 1;
        Command = cmd;
        Idx = 1;
        this.data = data;
    }

    /*
        解包函数，用于接收byte数据并格式化为包头结构
        @src 原始包头数据流
     */
    public void formatHead(byte[] src){
        if (src.length < 16) return;
        BinaryReadStream readStream  = new BinaryReadStream(src);
        PackageLen = readStream.readInt();
        HeadLen = readStream.readShort();
        Ver = readStream.readShort();
        Command = readStream.readInt();
        Idx = readStream.readInt();
    }

    /*
        解包函数，用于结构化接收byte数据（和构造函数功能相同）
        @src 原始包数据流
     */
    public void formatAll(byte[] src){
        if(src.length <= 16) return;
        BinaryReadStream readStream  = new BinaryReadStream(src);
        PackageLen = readStream.readInt();
        HeadLen = readStream.readShort();
        Ver = readStream.readShort();
        Command = readStream.readInt();
        Idx = readStream.readInt();
        data = readStream.readString();
    }

    /*
        封包函数，用户获取完整包数据
     */
    public byte [] getByte(){
        BinaryWriteStream writeStream = new BinaryWriteStream();
        writeStream.writeInt(PackageLen);   //PackageLen
        writeStream.writeShort(HeadLen);  //HeadLen
        writeStream.writeShort(Ver);    //Version
        writeStream.writeInt(Command);  //Command
        writeStream.writeInt(Idx);      //Idx
        writeStream.writeString(data);  //packagedata
        writeStream.flush();
        return writeStream.getData();
    }

    /*
        获取包总长度
     */
    public int getPackageLen(){
        return PackageLen;
    }

    /*
        获取包头长度
     */
    public short getHeadLen(){
        return HeadLen;
    }

    /*
        获取包版本号
     */
    public short getVer(){
        return Ver;
    }

    /*
        获取包命令码
     */
    public int getCommand(){
        return Command;
    }

    /*
        获取包序号
     */
    public int getIdx() {
        return Idx;
    }

    /*
        获取包体数据
     */
    public String getBody(){
        return data;
    }

    /*
        重设包体数据（加解密时使用）
     */
    public void setBody(String data){
        this.data = data;
        this.PackageLen = 16 + data.length();   //重新计算包大小
    }
}
